<?php

function emailTeam ($team, $booking, $comment, $user) {

    $customerList = "";

    $players = json_decode(get_field('players', $team), true);

    foreach ($players as $key => $value) {
        $customerList = $customerList."$value[name] <$value[email]>,";
    }

    $date = get_field('date',$booking);
    $slot = get_field('slot',$booking);

    $user_info = get_userdata($user);
    $subject = get_field('name', $team)." Team Lineup for PitchWise Booking - " . date('l, j F', strtotime($date))
        . ' from ' . get_field('start_time', $slot)
        . ' until ' . get_field('end_time', $slot);
    $message = '
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <p>Hello!</p>
    <p>A PitchWise game has been booked by '.$user_info->user_firstname.', and you\'re involved!</p>
    <p>The booking is confirmed for <strong>' . date('l, j F Y', strtotime($date)) . '</strong> from <strong>' . get_field('start_time', $slot) . '</strong> until <strong>' . get_field('end_time', $slot) . '</strong></p>
    <hr>
    <strong>'.get_field('name', $team).' Lineup</strong>
    <table style="margin-top:15px;">
    <thead>
        <tr style=""><th style="width:150px;">Team Colour</th><th style="width:150px;">Player Name</th></tr>
    </thead>
    <tbody>';

    foreach ($players as $key => $value) {
      $message = $message.'<tr><td style="text-align:center;"><img src="http://dev.pitchwise.co.uk/wp-content/themes/pitchwise/img/T-'.get_field('colour', $team).'.png" style="width:40px;height:40px;"></td>';
      $message = $message."<td style='text-align:center;'>$value[name]</td></tr>";
    }

    $message = $message.'</tbody></table><hr><strong>Comments from '.$user_info->user_firstname."</strong><p>$comment</p></body></html>";

    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'To: ' . $user_info->user_firstname . ' <' . $user_info->user_email . '>' . "\r\n";
    $headers .= 'Cc: ' . $customerList . "\r\n";
    $headers .= 'From: PitchWise <myteam@pitchwise.co.uk>' . "\r\n";
    //$headers .= 'Bcc: nazzanuk@gmail.com, nathan@pitchwise.co.uk, avneet@pitchwise.co.uk, kumar@pitchwise.co.uk, customer@pitchwise.co.uk' . "\r\n";
    $headers .= 'Bcc: nazzanuk@gmail.com' . "\r\n";

    mail($to, $subject, $message, $headers);


    return array('players'=> $players, 'customerList'=> $customerList);

}